(function() {
	// 查询条件box 查看选择 展开 点击方法 查询条件输入
	$J("#o_showQueryParam").click(function() {
		if (!$J("#o_queryParam").isDisplay()) {
			$J.hideSel();
			$J("#o_queryParam").slideDown("fast");
		} else {
			$J("#o_queryParam").slideUp("fast");
		}
		return false;
	});
	// 查询条件box点击方法 防止事件冒泡 鼠标在区域内点击 不相应document的点击方法而隐藏查询条件输入box
	$J("#o_queryParam").click(function(event){
		if(event.target == this){
			return false;
		}
	});
	// 查询条件box span文字点击方法 点击后显示输入框
	$J("#o_queryParam span.sel_text").live('click', function() {
		$J(this).parent().find('input.ui_input').show().focus();;
		$J(this).hide();
		return false;
	});
	// 查询条件box 输入框失去焦点后方法 失去焦点后把输入框内容赋值给span
	$J("#o_queryParam input.ui_input").live('blur', function() {
		if ($J(this).isBlank()) {
			$J(this).parent().find('span.sel_text').show().html($J(this).val());
		} else {
			$J(this).parent().find('span.sel_text').show().html($J(this).attr("placeholder"));
		}
		$J(this).hide();
	});
	// 查询条件box 输入框点击方法 阻止事件冒泡 防止点击输入框时触发document点击方法而隐藏box
	$J("#o_queryParam input.ui_input").live('click', function() {
		return false;
	});
	// 查询条件box 输入框键盘按下方法 tab键自定定位到下一行 enter键自动提交查询
	$J("#o_queryParam input.ui_input").live('keydown', function(event) {
		if (event.keyCode == 9) {
			if ($J(this).parent().next().find('span.sel_text').html()) {
				$J(this).parent().next().find('span.sel_text').click();
				$J(this).blur();
			} else {
				var firstSelText = $J(this).parent().parent().first().find('span.sel_text').attr("id");
				$J("#" + firstSelText).click();
			}
			return false;
		}
		if (event.keyCode == 13) {
			ordersQuery();
		}
	});
	
	// 排序方式 展开 点击方法
	$J("#o_showQuerySort").click(function() {
		if (!$J("#o_querySort").isDisplay()) {
			$J.hideSel();
			$J("#o_querySort").slideDown("fast");
		} else {
			$J("#o_querySort").slideUp("fast");
		}
		return false;
	});
	// 排序方式 内容 li 点击方法 点击后执行排序查询
	$J("#o_querySort li").live("click", function(){
		// data_current 当前是否选中 0否 1是
		if($J(this).attr("data_current") == 0){
			$J("#o_querySort li[data_current=1]").find("span.arrowDown").remove();
			$J("#o_querySort li[data_current=1]").find("span.arrowUp").remove();
			$J("#o_querySort li[data_current=1]").attr("data_current","0");
			
			$J(this).attr("data_current","1");
		}
		
		// data_val = ASC顺序 DESC倒序
		if($J(this).attr("data_val") == "ASC") {
			$J(this).attr("data_val","DESC");
			$J(this).find("span.arrowDown").remove();
			$J(this).prepend('<span class="arrowUp"></span>');
		} else {
			$J(this).attr("data_val","ASC");
			$J(this).find("span.arrowUp").remove();
			$J(this).prepend('<span class="arrowDown"></span>');
		}
		
		// 查询方法
		ordersQuery();
	});
	
	// 刷新按钮 点击方法
	$J("#o_queryFresh").click(function() {
		// 查询方法
		ordersQuery();
	});
	
	// 数据内容li点击方法 点击为选择当前li 更改背景颜色 同时查询对应商品信息
	$J("#o_list_ul li").live('click', function() {
		$J("#o_list_ul li.o_list_current").removeClass('o_list_current');
		$J(this).addClass('o_list_current');
		queryOrdersGoodsInfo(this);
	});
	// 数据内容li双击方法  显示订单详情
	$J("#o_list_ul li").live('dblclick', function() {
		$J(window).unload(HandleOnClose(true,"您输入的信息尚未保存，确定要离开本页吗？"));
		$J("#queryCenterColumn").css("left", $J(window).width());
		$J("#modifyOrder").css("left", "180px");
		
		var url = baseUrl + "/business/order/getOrderDetail.shtml";
		var param = {
			"orderid" : $J(this).attr("data_orderid")
		};
		var fnSuccess = {
			fn : function(data) {
				getOrderDetail(data);
			}
		};
		var fnError = {
			fn : function(){}
		};
		
		ajaxRequest(url, param, fnSuccess, fnError);
	});
	
	// 查询条件box 开始时间控件绑定
	$J('#q_begintime').DatePicker({
		date: $J('#q_begintime').val(),
		current: $J('#q_begintime').val(),
		starts: 1,
		position: 'bottom',
		onBeforeShow: function(){
			$J('#q_endtime').DatePickerHide();
			$J('#q_begintime').DatePickerSetDate($J('#q_begintime').val(), true);
		},
		onChange: function(formated, dates){
			$J('#q_begintime').val(formated);
			$J('#q_begintime').DatePickerHide();
			$J('#q_begintime').blur();
		}
	});
	// 查询条件box 结束时间控件绑定
	$J('#q_endtime').DatePicker({
		date: $J('#q_endtime').val(),
		current: $J('#q_endtime').val(),
		starts: 1,
		position: 'bottom',
		onBeforeShow: function(){
			$J('#q_begintime').DatePickerHide();
			$J('#q_endtime').DatePickerSetDate($J('#q_endtime').val(), true);
		},
		onChange: function(formated, dates){
			$J('#q_endtime').val(formated);
			$J('#q_endtime').DatePickerHide();
			$J('#q_endtime').blur();
		}
	});
	
	/**
	 * 查询订单商品信息 显示于右侧面板
	 */
	function queryOrdersGoodsInfo(e) {
		var orderid = $J(e).attr("data_orderid");
		var url = baseUrl + "/business/order/queryOrdersGoodsInfo.shtml";
		var param = {
			"orderid": orderid
		};
		var fnSuccess = {
			fn: function(data) {
				if (data && data.resultData.list) {
					$J("#o_right_goodsInfo").html("");
					var goodsList = data.resultData.list;
					for (var i = 0; i < goodsList.length; i++) {
						var html = "<ul>";
						html += "<li><span style='font-size: 18px;'>" + goodsList[i].goodsid + "</span></li>";
						html += "<li><label>款式：</label><span>" + goodsList[i].goodsstyle + "</span></li>";
						html += "<li><label>颜色：</label><span>" + goodsList[i].goodscolor + "</span></li>";
						html += "<li><label>数量：</label><span>" + goodsList[i].amount + "</span></li>";
						html += "<li><label>工序：</label><span>" + goodsList[i].process + "</span></li>";
						html += "<li><label>单价：</label><span>" + goodsList[i].price + "</span></li>";
						html += "<li><label>尺寸：</label><span style='display:block;'>" +
							goodsList[i].goodssize;
						html += "</ul>";
						
						$J("#o_right_goodsInfo").append(html);
					}
				} else {
					$J("#o_right_goodsInfo").html("<span style='color:#444;padding: 10px;display: block;'>无商品信息</span>");
				}
			}
		};
		var fnError = {
			fn: function() {}
		};

		ajaxRequest(url, param, fnSuccess, fnError);
	}
	
	/**
	 * 查询跳转方法
	 */
	function ordersQuery() {
		window.location.href = baseUrl + "/business/order/query.shtml" + ordersQueryParam();
	}
	
	/**
	 * 查询条件参数组成
	 */
	function ordersQueryParam() {
		var orderid = $J("#q_orderid").val();
		var customername = $J("#q_customername").val();
		var customeraddress = $J("#q_customeraddress").val();
		var customerphone = $J("#q_customerphone").val();
		var begintime = $J("#q_begintime").val();
		var endtime = $J("#q_endtime").val();
		var orderbyclause = $J("#o_querySort li[data_current=1]").attr("data_name") + " " + $J("#o_querySort li[data_current=1]").attr("data_val");
		var queryParam = "";

		if (orderid) {
			queryParam += "?orderid=" + orderid;
		}
		if (customername) {
			if (queryParam) {
				queryParam += "&customername=" + customername;
			} else {
				queryParam += "?customername=" + customername;
			}
		}
		if (customeraddress) {
			if (queryParam) {
				queryParam += "&customeraddress=" + customeraddress;
			} else {
				queryParam += "?customeraddress=" + customeraddress;
			}
		}
		if (customerphone) {
			if (queryParam) {
				queryParam += "&customerphone=" + customerphone;
			} else {
				queryParam += "?customerphone=" + customerphone;
			}
		}
		if (begintime) {
			if (queryParam) {
				queryParam += "&begintime=" + begintime;
			} else {
				queryParam += "?begintime=" + begintime;
			}
		}
		if (endtime) {
			if (queryParam) {
				queryParam += "&endtime=" + endtime;
			} else {
				queryParam += "?endtime=" + endtime;
			}
		}
		if (orderbyclause) {
			if (queryParam) {
				queryParam += "&orderbyclause=" + orderbyclause;
			} else {
				queryParam += "?orderbyclause=" + orderbyclause;
			}
		}
		return queryParam;
	}
	
	/**
	 * 获取订单相信信息
	 * @param data ajax查询出来的数据
	 */
	function getOrderDetail(data) {
		$J("#orderkeyid").val(data.resultData.order.id);
		$J("#orderid").val(data.resultData.order.orderid);
		$J("#agents").val(data.resultData.order.agents);
		$J("#customername").val(data.resultData.order.customername);
		$J("#customerphone").val(data.resultData.order.customerphone);
		$J("#customeraddress").val(data.resultData.order.customeraddress);
		$J("#paidmoney").val(data.resultData.order.paidmoney);
		$J("#allmoney").val(data.resultData.order.allmoney);
		$J("#isExpedited_sel ul.m_sel").selectedOp(data.resultData.order.isexpedited);
		$J("#process_sel ul.m_sel").selectedOp(data.resultData.order.process);
		$J("#begintime").val(DateUtil.subDate(data.resultData.order.begintime));
		$J("#endtime").val(DateUtil.subDate(data.resultData.order.endtime));
		$J("#remark").val(data.resultData.order.remark);
		
		goodsDetailHtml(data.resultData.goods);
	}

	/**
	 * 组成订单商品信息html内容
	 * @param data 商品信息集合
	 * @returns {String} 组成好后的html内容
	 */
	function goodsDetailHtml(data) {
		$J("#o_create_goods_list").html("");
		if (data) {
			for (var i = 0; i < data.length; i++) {
				var html = '<ul class="o_goods_detail">' +
					'<li>' +
					'<h3>编号</h3>' +
					'<a href="javascript:void(0);"><span class="delIcon" style="position: absolute;top: 5px;left: 222px;"></span></a>' +
					'</li>' +
					'<li>' +
					'<label>商品编号</label>' +
					'<input class="ui_input" data_name="id" type="text" value="' + data[i].id + '" style="display: none;">' +
					'<input class="ui_input" data_name="isRemove" type="text" value="0" style="display: none;">' +
					'<input class="ui_input" data_name="goodsid" type="text" value="' + data[i].goodsid + '" disabled placeholder="商品编号">' +
					'</li>' +
					'<li>' +
					'<h3>商品详情</h3>' +
					'</li>' +
					'<li>' +
					'<label>材质</label>' +
					'<input class="ui_input" data_name="goodsmaterial" type="text" placeholder="商品材质" value="' + data[i].goodsmaterial + '">' +
					'</li>' +
					'<li>' +
					'<label>颜色</label>' +
					'<input class="ui_input" data_name="goodscolor" type="text" placeholder="商品颜色" value="' + data[i].goodscolor + '">' +
					'</li>' +
					'<li>' +
					'<label>款式</label>' +
					'<input class="ui_input" data_name="goodsstyle" type="text" placeholder="商品款式" value="' + data[i].goodsstyle + '">' +
					'</li>' +
					'<li>' +
					'<label>数量</label>' +
					'<input class="ui_input" data_name="amount" type="text" placeholder="商品数量" value="' + data[i].amount + '">' +
					'</li>' +
					'<li>' +
					'<label>单价</label>' +
					'<input class="ui_input" data_name="price" type="text" placeholder="商品单价" value="' + data[i].price + '">' +
					'</li>' +
					'<li>' +
					'<label>工序</label>' + selectHtml.goodsProcess(data[i].process) +
					'</li>' +
					'<li>' +
					'<label>类型</label>' + selectHtml.goodsType(data[i].goodstype) +
					'</li>' +
					'<li>'+
					'<label>尺寸</label>'+
					'<input class="ui_input" data_name="goodssize" type="text" style="margin-right: 4px;" placeholder="尺寸" validate="required" value="'+ data[i].goodssize +'">'+
					'</li>'+
					'<li>'+
					'<label>图片</label>'+
					'<input class="ui_input" data_name="goodspic" type="text" style="margin-right: 4px;" placeholder="图片">'+
					'</li>'+
					'<label>备注</label>' +
					'<textarea class="ui_input" data_name="remark" rows="4" placeholder="备注">' + data[i].remark + '</textarea>' +
					'</li>' +
					'</ul>';
				var goodsDetail = $J("#o_create_goods_list").append(html);
				goodsDetail.find("div.d_sel").selOp();
				goodsDetail.find("div[data_name='process']").selectedOp(data[i].process);
				goodsDetail.find("div[data_name='goodstype']").selectedOp(data[i].goodstype);
			};
		}
	}
})();